home *** CD-ROM | disk | FTP | other *** search
/ Young Minds / Young Minds Interactive CD-ROM.ISO / backgamm / save.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-03-09  |  3.3 KB  |  160 lines

  1. /*
  2.  * Copyright (c) 1980 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted
  6.  * provided that this notice is preserved and that due credit is given
  7.  * to the University of California at Berkeley. The name of the University
  8.  * may not be used to endorse or promote products derived from this
  9.  * software without specific prior written permission. This software
  10.  * is provided ``as is'' without express or implied warranty.
  11.  */
  12.  
  13. #ifndef lint
  14. static char sccsid[] = "@(#)save.c    5.2 (Berkeley) 2/16/88";
  15. #endif /* not lint */
  16.  
  17. #include "back.h"
  18.  
  19. extern int    errno;
  20.  
  21. static char    confirm[] = "Are you sure you want to leave now?";
  22. static char    prompt[] = "Enter a file name:  ";
  23. static char    exist1[] = "The file '";
  24. static char    exist2[] =
  25.     "' already exists.\nAre you sure you want to use this file?";
  26. static char    cantuse[] = "\nCan't use ";
  27. static char    saved[] = "This game has been saved on the file '";
  28. static char    type[] = "'.\nType \"backgammon ";
  29. static char    rec[] = "\" to recover your game.\n\n";
  30. static char    cantrec[] = "Can't recover file:  ";
  31.  
  32. save (n)
  33. register int    n;
  34.  
  35. {
  36.     register int    fdesc;
  37.     register char    *fs;
  38.     char        fname[50];
  39.  
  40.     if (n)  {
  41.         if (tflag)  {
  42.             curmove (20,0);
  43.             clend();
  44.         } else
  45.             writec ('\n');
  46.         writel (confirm);
  47.         if (! yorn(0))
  48.             return;
  49.     }
  50.     cflag = 1;
  51.     for (;;)  {
  52.         writel (prompt);
  53.         fs = fname;
  54.         while ((*fs = readc()) != '\n')  {
  55.             if (*fs == tty.sg_erase)  {
  56.                 if (fs > fname)  {
  57.                     fs--;
  58.                     if (tflag)
  59.                         curmove (curr,curc-1);
  60.                     else
  61.                         writec (*fs);
  62.                 } else
  63.                     writec ('\007');
  64.                 continue;
  65.             }
  66.             writec (*fs++);
  67.         }
  68.         *fs = '\0';
  69.         if ((fdesc = open(fname,2)) == -1 && errno == 2)  {
  70.             if ((fdesc = creat (fname,0700)) != -1)
  71.             break;
  72.         }
  73.         if (fdesc != -1)  {
  74.             if (tflag)  {
  75.                 curmove (18,0);
  76.                 clend();
  77.             } else
  78.                 writec ('\n');
  79.             writel (exist1);
  80.             writel (fname);
  81.             writel (exist2);
  82.             cflag = 0;
  83.             close (fdesc);
  84.             if (yorn (0))  {
  85.                 unlink (fname);
  86.                 fdesc = creat (fname,0700);
  87.                 break;
  88.             } else  {
  89.                 cflag = 1;
  90.                 continue;
  91.             }
  92.         }
  93.         writel (cantuse);
  94.         writel (fname);
  95.         writel (".\n");
  96.         close (fdesc);
  97.         cflag = 1;
  98.     }
  99.     write (fdesc,board,sizeof board);
  100.     write (fdesc,off,sizeof off);
  101.     write (fdesc,in,sizeof in);
  102.     write (fdesc,dice,sizeof dice);
  103.     write (fdesc,&cturn,sizeof cturn);
  104.     write (fdesc,&dlast,sizeof dlast);
  105.     write (fdesc,&pnum,sizeof pnum);
  106.     write (fdesc,&rscore,sizeof rscore);
  107.     write (fdesc,&wscore,sizeof wscore);
  108.     write (fdesc,&gvalue,sizeof gvalue);
  109.     write (fdesc,&raflag,sizeof raflag);
  110.     close (fdesc);
  111.     if (tflag)
  112.         curmove (18,0);
  113.     writel (saved);
  114.     writel (fname);
  115.     writel (type);
  116.     writel (fname);
  117.     writel (rec);
  118.     if (tflag)
  119.         clend();
  120.     getout ();
  121. }
  122.  
  123. recover (s)
  124. char    *s;
  125.  
  126. {
  127.     register int    i;
  128.     int        fdesc;
  129.  
  130.     if ((fdesc = open (s,0)) == -1)
  131.         norec (s);
  132.     read (fdesc,board,sizeof board);
  133.     read (fdesc,off,sizeof off);
  134.     read (fdesc,in,sizeof in);
  135.     read (fdesc,dice,sizeof dice);
  136.     read (fdesc,&cturn,sizeof cturn);
  137.     read (fdesc,&dlast,sizeof dlast);
  138.     read (fdesc,&pnum,sizeof pnum);
  139.     read (fdesc,&rscore,sizeof rscore);
  140.     read (fdesc,&wscore,sizeof wscore);
  141.     read (fdesc,&gvalue,sizeof gvalue);
  142.     read (fdesc,&raflag,sizeof raflag);
  143.     close (fdesc);
  144.     rflag = 1;
  145. }
  146.  
  147. norec (s)
  148. register char    *s;
  149.  
  150. {
  151.     register char    *c;
  152.  
  153.     tflag = 0;
  154.     writel (cantrec);
  155.     c = s;
  156.     while (*c != '\0')
  157.         writec (*c++);
  158.     getout ();
  159. }
  160.